/**
Zeke Elder - zbe@andrew.cmu.edu
Amal Chemmany - achemman@andrew.cmu.edu
Nishant Kumar - nkumar1@andrew.cmu.edu
15-637
4/2/2012
 **/

package edu.cmu.fithub.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.mybeans.form.FormBeanFactory;

import edu.cmu.fithub.controller.Action;
import edu.cmu.fithub.db.DBHelper;
import edu.cmu.fithub.db.bean.Group;
import edu.cmu.fithub.db.bean.GroupMembers;
import edu.cmu.fithub.db.bean.User;
import edu.cmu.fithub.formbeans.LoginForm;

public class DisplayGroupAction extends Action {

	private FormBeanFactory<LoginForm> formFactory = FormBeanFactory
			.getInstance(LoginForm.class);

	public DisplayGroupAction() {
	}

	public String getName() {
		return "showGroups.do";
	}

	public String perform(HttpServletRequest request) {

		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors", errors);

		User user = (User) request.getSession().getAttribute("user");
		Session dbSession = DBHelper.getSessionFactory().openSession();
		List subscribedGroups = dbSession.createCriteria(GroupMembers.class)
				.add(Restrictions.eq("userId", user.getUserId())).list();
		GroupMembers[] groupList = new GroupMembers[subscribedGroups.size()];
		groupList = (GroupMembers[]) subscribedGroups.toArray(groupList);
		request.setAttribute("subscribedGroups", groupList);

		return "groups.jsp";
	}
}
